import message from '@/logic/message';
import {AppModule} from './AppModule';
import {PageModule} from './PageModule';


class Loader{
    constructor() {
        this.staticModules={};
    }
    //加载静态资源
    loadResource(opts) {
        const {appId,bridgeId,pages,platform,currProjectPath}=opts
        let logicResourcePath='';
        if(platform=='app'){
            // let baseUrl=`http://local.mp.cn`;
            logicResourcePath=currProjectPath+`/apps/${appId}/logic.js`;
        }else{
            logicResourcePath=currProjectPath+`/logic.js`;
        }
        importScripts(logicResourcePath);


        //加载app模块
        modRequire('app');

        //加载页面模块
        pages.forEach(pagePath=>{
            modRequire(pagePath);
        })

        message.send({
            type:'logicResourceLoaded',
            body:{
                bridgeId
            }
        })

    }

    createAppModule(mobuleInfo){
        const appModule=new AppModule(mobuleInfo);
        // console.log("appModule:",appModule)
        this.staticModules['app']=appModule;
    }

    createPageModule(mobuleInfo,compileInfo){
        const pageModule=new PageModule(mobuleInfo,compileInfo);
        console.log("pageModule:",pageModule)
        const {path}=compileInfo;
        this.staticModules[path]=pageModule;
    }

    //根据页面路径获取初始数据
    getInitialDataByPagePath(pagePath){
        const pageModule=this.staticModules[pagePath];
        console.log("pageModule2:",pageModule)
        return {
            [pagePath]:pageModule.getInitialData()
        }
    }
    
    getModuleByPath(path){
        return this.staticModules[path];
    }
    
    
}

export default new Loader();